import { Room } from './Room';
import { Card } from '../card/base/Card';
import { AIState } from '../ai/AIState';

export interface IDeptData {
	text: string;
	value: number;
	unit: string;
}

export class Dept {
	constructor(readonly deptId: string) {
		this.ai = new AIState(this);
	}
	ai: AIState;
	//是否是电脑
	isAI: boolean = true;
	// 是否结束回合
	round: boolean = false;
	// 玩家ID
	playerId: string = '';
	// 部门名称
	name: string = '未命名部门';
	// 部门贴图
	icon: string = '';
	// 部门手牌
	fieldCards: Card[] = [];
	handCards: Card[] = [];
	// 部门行动点
	point: number = 0;
	// 部门最大行动点
	pointMax: number = 0;
	// 部门势力
	power: number = 0;
	// 部门数据
	data: Record<string, IDeptData> = {};
	/**
	 * 设置部门数据
	 * @param id
	 * @param deptData
	 * @returns
	 */
	setDeptData(id: string, deptData: IDeptData): this {
		this.data[id] = deptData;
		return this;
	}
	// 获取部门数据
	getDeptDataValue(name: string) {
		const data = this.data[name];
		if (!data) return 0;
		return data.value;
	}
	// 添加部门数据数值
	addDeptDataValue(name: string, value: number) {
		const data = this.data[name];
		if (!data) return;
		data.value += value;
		data.value = Math.max(data.value, 0);
	}
	/**
	 * 前后端传输并不能传输class，所以我们需要将其转换为JSON
	 * @param room
	 * @returns
	 */
	toClient(room: Room) {
		const handCards = [];
		for (const i in this.handCards) {
			handCards.push(this.handCards[i].toClient(room, this.deptId));
		}
		const fieldCards = [];
		for (const i in this.fieldCards) {
			fieldCards.push(this.fieldCards[i].toClient(room, this.deptId));
		}
		const data = this.data;
		const icon = this.icon;
		const name = this.name;
		const point = this.point;
		const pointMax = this.pointMax;
		const power = this.power;
		const deptId = this.deptId;
		return {
			deptId,
			handCards,
			fieldCards,
			data,
			icon,
			name,
			point,
			pointMax,
			power,
		};
	}
}
